package com.css.common.core.enums;

import cn.hutool.core.util.NumberUtil;
import com.css.common.core.utils.BaseUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;

@Getter
@AllArgsConstructor
public enum YwlxEnum {

    YWLX_TX("0000", "提现", "", "", "", "", null, null, "", false),

    YWLX_JCTY("0001", "基础通用", "", "", "", "", null, NumberUtil.toBigDecimal(1), "", false),

    YWLX_CDZ("0002", "充电桩", "", "", "", "", null, NumberUtil.toBigDecimal(1), "", false),

    YWLX_SC("0101", "商超", "", "", "", "", null, null, "", false),

    YWLX_CZC("0201", "出租车", "09", "出租车客运服务费", "运输服务", "3019901000000000000", null, NumberUtil.toBigDecimal(1), "0.01", false),

    YWLX_CTQC("0202", "公路客运", "09", "客运服务费", "运输服务", "3019901000000000000", null, NumberUtil.toBigDecimal(1), "0.03", false),

    YWLX_YC("0203", "水路客运", "09", "游船服务费", "运输服务", "3010201010000000000", null, NumberUtil.toBigDecimal(1), "0.09", false),

    YWLX_LKYS("0200", "旅客运输", "", "", "", "", null, null, "", false),

    YWLX_TCC("0301", "停车场", "06", "车辆停放服务", "经营租赁", "3040502020200000000", "平方米", NumberUtil.toBigDecimal(12), "0.05", false),

    YWLX_LYJD("0401", "旅游景点", null, "普通门票", "旅游服务", "3070301000000000000", "张", NumberUtil.toBigDecimal(1), "0.06", false),

    YWLX_CPY("0601", "加油站", "01", "汽油", "汽油", "1070101010100000000", "升", NumberUtil.toBigDecimal(1), "0.13", false),

    YWLX_NCP("0701", "农产品收购", "16", "农产品", "农产品", "", "千克", NumberUtil.toBigDecimal(1), "0.00", true),

    YWLX_ZSZYHS("5101", "废品回收", "24", "再生资源回收", "再生资源回收", "1110701000000000000", "吨", NumberUtil.toBigDecimal(1), "0.05", true),

    YWLX_LHYG("5201", "灵活用工", "25", "", "", "", null, null, "", true),

    YWLX_WLHY("5202", "网络货运（灵工）", "25", "", "", "", null, null, "", true);
    private final String dm;

    private final String mc;

    /**
     * 特定要素码
     */
    private final String tdys;

    /**
     * 项目名称
     */
    private final String xmmc;

    /**
     * 商品服务简称
     */
    private final String spfwjc;

    /**
     * 货物或应税劳务、服务名称
     */
    private final String sphfwflbm;

    /**
     * 单位
     */
    private final String dw;

    /**
     * 数量
     */
    private final BigDecimal sl;

    /**
     * 征收税率
     */
    private final String slzsl;

    /**
     * 是否反向开票
     */
    private final boolean isFxkp;

    /**
     * 判断是否为当前业务类型
     */
    public boolean equals(String dm) {
        return this.dm.equals(dm);
    }

    public static String getMc(String dm) {
        YwlxEnum res = Arrays.stream(values())
                .filter(x -> x.getDm().equals(dm))
                .findFirst().orElse(null);
        return res == null ? null : res.getMc();
    }

    public static YwlxEnum getInstance(String dm) {
        for (YwlxEnum value : values()) {
            if (Objects.equals(dm, value.getDm())) {
                return value;
            }
        }
        return null;
    }

    /**
     * 获取反向开票业务类型
     *
     * @param dm
     * @return
     */
    public static YwlxEnum getFxkpYwlx(String dm) {
        if (BaseUtil.isNull(dm)) return null;
        if (!dm.equals(YwlxEnum.YWLX_NCP.getDm()) && !dm.substring(0, 1).equals("5")) return null;//反向开票业务类型位5开头
        return YwlxEnum.getInstance(dm);
    }

    /**
     * 根据订单id获取业务类型代码
     *
     * @param ddid
     * @return
     */
    public static String getYwlxByDdid(String ddid) {
        if (BaseUtil.isNull(ddid)) return null;
        int length = ddid.length();
        if (length >= 23 && !"20".equals(ddid.substring(4, 6))) {//用4位年的为历史数据
            //截取20 - 23位
            return ddid.substring(19, 23);
        }
        return null;
    }


}
